package io.yuncheng.modules.sys.controller;

import io.yuncheng.common.exception.RRException;
import io.yuncheng.common.utils.Constant;
import io.yuncheng.common.utils.PageUtils;
import io.yuncheng.common.utils.R;
import io.yuncheng.modules.sys.entity.*;
import io.yuncheng.modules.sys.entity.*;
import io.yuncheng.modules.sys.service.SubAppLogCollectService;
import io.yuncheng.modules.sys.service.SubApplicationService;
import io.yuncheng.modules.sys.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


/**
 * 调用日志汇总
 *
 * @author jared
 * @email 15721509625@163.com
 * @date 2020-11-28 17:28:02
 */
@RestController
@Api(tags = "日志汇总")
@RequestMapping("sys/subapplogcollect")
public class SubAppLogCollectController {
    @Autowired
    private SubAppLogCollectService subAppLogCollectService;
    @Autowired
    private SubApplicationService subApplicationService;
    @Resource
    private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:subapplogcollect:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = subAppLogCollectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:subapplogcollect:info")
    public R info(@PathVariable("id") Long id) {
        SubAppLogCollectEntity subAppLogCollect = subAppLogCollectService.getById(id);

        return R.ok().put("subAppLogCollect", subAppLogCollect);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:subapplogcollect:save")
    public R save(@RequestBody SubAppLogCollectEntity subAppLogCollect) {
        subAppLogCollectService.save(subAppLogCollect);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:subapplogcollect:update")
    public R update(@RequestBody SubAppLogCollectEntity subAppLogCollect) {
        subAppLogCollectService.updateById(subAppLogCollect);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:subapplogcollect:delete")
    public R delete(@RequestBody Long[] ids) {
        subAppLogCollectService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     *大数据局省级数据
     */
    @GetMapping("/total-by-dept")
    @ApiOperation("列表-单个部门数据")
    public R dataByDeptId(@RequestParam("deptId") Long deptId) {
        Long appId = null;
        if(deptId!=null){
            SubApplicationEntity subApplicationEntity = subApplicationService.queryByDeptId(deptId);
            if(subApplicationEntity!=null){
                appId = subApplicationEntity.getAppId();
            }
        }
        return R.ok().put("data", subAppLogCollectService.queryByAppId(appId));
    }
    /**
     *大数据局省级数据
     */
    @PostMapping("/total")
    @ApiOperation("大数据局省级数据")
    public R totalProvince(@RequestBody SubApplicationEntity entity)  {
        List<SysDeptEntity> sysDeptEntities = sysDeptService.queryListByType(Constant.DeptTypeEnum.PROVINCE.getValue());
        if(sysDeptEntities == null){
            throw  new RRException("请检查省级机构配置");
        }
        List<SubApplicationEntity> subApplicationEntities = subApplicationService.queryConfirmListByRootDeptIdAndType(sysDeptEntities.get(0).getDeptId(),entity.getAppType());
        SubAppLogCollectObjectVo vo = subAppLogCollectService.queryTotalByEntity(subApplicationEntities);
        return R.ok().put("data", vo.buildReturnVo());
    }
    /**
     *  密码局省级数据
     */
    @PostMapping("/city-total")
    @ApiOperation("密码局省级数据")
    public R totalCity(@RequestBody SubApplicationEntity entity) {
        List<SysDeptEntity> sysDeptEntities = sysDeptService.queryListByType(Constant.DeptTypeEnum.PROVINCE.getValue());
        if(sysDeptEntities == null){
            throw  new RRException("请检查省级机构配置");
        }
        List<SubApplicationEntity> province = subApplicationService.queryConfirmListByRootDeptIdAndType(sysDeptEntities.get(0).getDeptId(), entity.getAppType());
        List<SysDeptEntity> cityDepts = sysDeptService.queryListByType(Constant.DeptTypeEnum.CITY.getValue());
        List<SubAppLogCollectVo> cityTotalList = new ArrayList<>();
        SubAppLogCollectObjectVo provinceVo = subAppLogCollectService.queryTotalByEntity(province);
        for (SysDeptEntity cityDept : cityDepts) {
            List<SubApplicationEntity> city = subApplicationService.queryConfirmListByRootDeptIdAndType(cityDept.getDeptId(), entity.getAppType());
            SubAppLogCollectObjectVo cityVo = subAppLogCollectService.queryTotalByEntity(city);
            SubAppLogCollectVo cityTotal = SubAppLogCollectVo.initBuildInterface(cityDept.getName());
            cityTotal.addTotalList(cityVo.getCallTotal());
            cityTotal.addTotalList(cityVo.getAppNum());
            cityTotal.addTotalList(cityVo.getLoginUser());
            cityTotal.addTotalList(cityVo.getInUseNum());
            cityTotal.addTotalList(cityVo.getEncryptionNum());
            cityTotalList.add(cityTotal.cleanMap());
            provinceVo.addTotal(cityVo);

        }
        return R.ok().put("data", provinceVo.buildReturnVo()).put("city",cityTotalList);
    }


}
